import { _decorator, Component, Node,tween,Vec3,v3 } from 'cc';
import {GameConfig} from "./GameConfig";
const { ccclass, property } = _decorator;

@ccclass('Rocket')
export class Rocket extends Component {

    public rocketUp:Node=null;
    public rocketDown:Node=null;
    public rocketLeft:Node=null;
    public rocketRight:Node=null;

    public inited:boolean=false;

    onLoad()
    {
        this.init();
    }

    start() {

    }

    public init()
    {
        if(this.inited)
            return;
        this.rocketUp=this.node.getChildByName("rocketUp");
        this.rocketDown=this.node.getChildByName("rocketDown");
        this.rocketLeft=this.node.getChildByName("rocketLeft");
        this.rocketRight=this.node.getChildByName("rocketRight");
        this.inited=true;
    }

    public lanch()
    {
        this.init();
        tween(this.node).to(0.6, {scale:Vec3.ONE },{easing:"backOut"}).call(()=>{
            this.rocketMove();
        }).start();
    }

    public rocketMove()
    {
        let pos:Vec3=this.rocketUp.getPosition();
        tween(this.rocketUp).to(1.2, { position: v3(pos.x, pos.y + 1600,pos.z)}).call(()=>{

        }).start();

        pos=this.rocketDown.getPosition();
        tween(this.rocketDown).to(1.2, { position: v3(pos.x, pos.y - 1600,pos.z)}).call(()=>{

        }).start();

        pos=this.rocketLeft.getPosition();
        tween(this.rocketLeft).to(1.2, { position: v3(pos.x-1600, pos.y, pos.z)}).call(()=>{

        }).start();

        pos=this.rocketRight.getPosition();
        tween(this.rocketRight).to(1.2, { position: v3(pos.x+1600, pos.y, pos.z)}).call(()=>{
            this.destroySelf();
        }).start();
    }

    public destroySelf()
    {
        this.node.removeFromParent(true);
        this.node.destroy();
    }

    update(deltaTime: number) {
        
    }
}

